﻿using Common.Contract;
using Common.Contract.HuntingCustomer;
using Common.Contract.HuntingCustomer.Record;
using Common.Contract.HuntingCustomer.Request;
using Common.Contract.HuntingCustomer.Response;
using Common.Contract.Paging;
using Common.Service;
using Common.Service.DataTables;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Models.ViewModels.HuntingCustomer;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using System.Globalization;
using Common.Contract.SystemConfig.Record;
using SMECustomerWebsite.Filters;

namespace SMECustomerWebsite.Controllers
{
    public class HuntingCustomerController : ControllerBase
    {
        //
        // GET: /HuntingCustomer/
        [CustomActionFilter]
        [Authorize]
        public ActionResult Index()
        {
            HuntingCustomerViewModel vm = new HuntingCustomerViewModel
            {
                CurrentUser = CurrentUserRecord
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CustHuntingData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<IHuntingCustomer> serviceClient = new ServiceClient<IHuntingCustomer>())
            {
                FilterRecord filter = GetBaseFilter(pageRequest.SearchColumns);
                filter.Industry = null;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        filter.Description = pageRequest.SearchColumns[5];
                        filter.ProductCategory = pageRequest.SearchColumns[6];
                        filter.StatusAccPlan = pageRequest.SearchColumns[7];
                        filter.CusOpenDate = pageRequest.SearchColumns[8];
                        break;
                    case "2":
                        filter.Description = pageRequest.SearchColumns[4];
                        filter.ProductCategory = pageRequest.SearchColumns[5];
                        filter.StatusAccPlan = pageRequest.SearchColumns[6];
                        filter.CusOpenDate = pageRequest.SearchColumns[7];
                        break;
                    case "3":
                        filter.Description = pageRequest.SearchColumns[3];
                        filter.ProductCategory = pageRequest.SearchColumns[4];
                        filter.StatusAccPlan = pageRequest.SearchColumns[5];
                        filter.CusOpenDate = pageRequest.SearchColumns[6];
                        break;
                    case "4":
                        filter.Description = pageRequest.SearchColumns[1];
                        filter.ProductCategory = pageRequest.SearchColumns[2];
                        filter.StatusAccPlan = pageRequest.SearchColumns[3];
                        filter.CusOpenDate = pageRequest.SearchColumns[4];
                        break;
                    default:
                        break;
                }
                RetrieveCustomerHuntingByDaoRequest request = new RetrieveCustomerHuntingByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustFeedbackData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Lang = CurrentLang,
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerHuntingByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerHuntingByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<CustomerHuntingRecord>() : customerRecords.Records;
                if (resultCustomer != null && resultCustomer.Count > 0)
                {
                    switch (CurrentUserRecord.GroupIdentifier)
                    {
                        case "4":
                            pageResponse = new DataTableResponse
                            {
                                sEcho = pageRequest.Echo,
                                iTotalRecords = customerRecords.Page.TotalRecords,
                                sColumns = pageRequest.ColumnNames,
                                aaData = resultCustomer.Select(x => new object[]{
                                                             x.CompanyName,
                                                             x.CompanyInfo,
                                                             x.ProductCategory,
                                                             x.ContactStatus,
                                                             x.Modifiedtime.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                           }).ToList()
                            };
                            break;
                        case "3":
                            pageResponse = new DataTableResponse
                            {
                                sEcho = pageRequest.Echo,
                                iTotalRecords = customerRecords.Page.TotalRecords,
                                sColumns = pageRequest.ColumnNames,
                                aaData = resultCustomer.Select(x => new object[]{
                                                             x.Dao,
                                                             x.UserDaoName,
                                                             x.CompanyName,
                                                             x.CompanyInfo,
                                                             x.ProductCategory,
                                                             x.ContactStatus,
                                                             x.Modifiedtime.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                           }).ToList()
                            };
                            break;
                        case "2":
                            pageResponse = new DataTableResponse
                            {
                                sEcho = pageRequest.Echo,
                                iTotalRecords = customerRecords.Page.TotalRecords,
                                sColumns = pageRequest.ColumnNames,
                                aaData = resultCustomer.Select(x => new object[]{
                                                             x.BranchNameSme,
                                                             x.Dao,
                                                             x.UserDaoName,
                                                             x.CompanyName,
                                                             x.CompanyInfo,
                                                             x.ProductCategory,
                                                             x.ContactStatus,
                                                             x.Modifiedtime.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                           }).ToList()
                            };
                            break;
                        case "1":
                            pageResponse = new DataTableResponse
                            {
                                sEcho = pageRequest.Echo,
                                iTotalRecords = customerRecords.Page.TotalRecords,
                                sColumns = pageRequest.ColumnNames,
                                aaData = resultCustomer.Select(x => new object[]{
                                                             x.Region,
                                                             x.BranchNameSme,
                                                             x.Dao,
                                                             x.UserDaoName,
                                                             x.CompanyName,
                                                             x.CompanyInfo,
                                                             x.ProductCategory,
                                                             x.ContactStatus,
                                                             x.Modifiedtime.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture),
                                                             x.Leadid
                                                           }).ToList()
                            };
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    pageResponse = new DataTableResponse
                    {
                        sEcho = pageRequest.Echo,
                        iTotalRecords = 0,
                        sColumns = pageRequest.ColumnNames,
                        aaData = new List<object[]>()
                    };
                }
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RelationCustomerInfo(string lead)
        {
            CustomerHuntingRecord resultCustomer = new CustomerHuntingRecord();
            using (ServiceClient<IHuntingCustomer> serviceClient = new ServiceClient<IHuntingCustomer>())
            {
                RetrieveRelationCustomerInfoByLeadIdRequest request = new RetrieveRelationCustomerInfoByLeadIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustomerInfo"
                    },
                    LeadId = lead,
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveRelationCustomerInfoByLeadIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveRelationCustomerInfoByLeadId(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new CustomerHuntingRecord() : customerRecords.Record;
            }
            var objectItem = new
            {
                address = resultCustomer.CompanyAddress,
                custName = resultCustomer.CompanyName,
                email = resultCustomer.CompanyEmail,
                phone = resultCustomer.CompanyPhone,
                relemail = resultCustomer.RelationEmail,
                relphone = resultCustomer.RelationPhone,
                relName = resultCustomer.RelationName,
                description = resultCustomer.CompanyInfo
            };
            return Json(objectItem);
        }

    }
}
